<template>
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width: 80%">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="试卷名称" prop="paperName">
          <el-input
            v-model="queryParams.paperName"
            placeholder="请输入试卷名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="试卷类型" prop="paperType">
          <el-select
            v-model="queryParams.paperType"
            placeholder="请选择试卷类型"
            clearable
            class="!w-240px"
          >
            <el-option v-for="item in paperTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="queryParams.createTime"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
            class="!w-240px"
          />
        </el-form-item>

        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
          <el-button
            type="primary"
            plain
            @click="openForm('create')"
            v-hasPermi="['exam:paper:create']"
          >
            <Icon icon="ep:plus" class="mr-5px" /> 新增
          </el-button>
          <el-button
            type="success"
            plain
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['exam:paper:export']"
          >
            <Icon icon="ep:download" class="mr-5px" /> 导出
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" @selection-change="handleSelectionChange" ref="multipleTableRef">
        <el-table-column type="selection" width="55"/>
        <el-table-column label="试卷名称" align="center" prop="paperName" />
        <el-table-column label="考试时长（分钟）" align="center" prop="duration" />
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          width="180px"
        />
        <el-table-column label="试卷类型" align="center" prop="paperType" >
          <template #default="scope">
            <el-tag v-if="scope.row.paperType===1">
              随机试卷
            </el-tag>
            <el-tag v-if="scope.row.paperType===0">
              固定出题
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="试卷总分" align="center" prop="totalScore" />
        <el-table-column label="及格线" align="center" prop="passingScore" />
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="openForm('update', scope.row.id)"
              v-hasPermi="['exam:paper:update']"
            >
              编辑
            </el-button>
            <el-button
              link
              type="danger"
              @click="handleDelete(scope.row.id)"
              v-hasPermi="['exam:paper:delete']"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

  </el-dialog>
</template>
<script setup lang="ts">
import {PaperApi, PaperVO} from "@/api/exam/paper";
import {ElTable} from "element-plus";
const {t} = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formRef = ref() // 表单 Ref
const paperTypeOptions=ref([{label:'随机试卷',value:0},{label:'固定出题',value:1}]);
const loading = ref(true) // 列表的加载中
const list = ref<PaperVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  paperName: undefined,
  createTime: [],
  paperType: undefined,
})
function reSetQueryParams(){
  queryParams.pageNo = 1;
  queryParams.pageSize = 10;
  queryParams.paperName = undefined;
  queryParams.createTime = [];
  queryParams.paperType = undefined;
}
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  reSetQueryParams();
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      await getList();
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({open}) // 提供 open 方法，用于打开弹窗
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {

  // 提交请求
  formLoading.value = true
  try {
    dialogVisible.value = false
    emit('selectPaper',45678)
  } finally {
    formLoading.value = false
  }
}
const multipleTableRef = ref<InstanceType<typeof ElTable>>()
function handleSelectionChange(val){
  try {
    if(val&&val.length>0){
      message.confirm('确认选择试题'+val[0].paperName,'试卷选择').then((re)=>{
        dialogVisible.value = false
        emit('selectPaper',val);
      });
      multipleTableRef.value!.clearSelection()
    }
  }finally {
    formLoading.value = false
  }
}

const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PaperApi.getPaperPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

</script>
